package com.note.mq.configure;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    @Autowired
    RabbitMqProperties rabbitMqProperties;


    @Bean
    FanoutExchange directFileUrlExchange() {
        return new FanoutExchange(rabbitMqProperties.getFileurlexchangename());
    }


    @Bean
    FanoutExchange directNoteExchange() {
        return new FanoutExchange(rabbitMqProperties.getExchangename());
    }

    // 声明死信Exchange
    @Bean
    public FanoutExchange deadLetterExchange(){
        return new FanoutExchange(rabbitMqProperties.getDeadexchangename());
    }

    @Bean
    public Queue directNoteQueue() {
        Map<String, Object> args = new HashMap<>(2);
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", rabbitMqProperties.getDeadexchangename());
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
//        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUEB_ROUTING_KEY);
        //note队列和死信exchange进行绑定
        return QueueBuilder.durable(rabbitMqProperties.getQueuename()).withArguments(args).build();
    }

    // 声明死信队列
    @Bean
    public Queue deadLetterQueue(){
        return new Queue(rabbitMqProperties.getDeadqueuename());
    }

    @Bean
    public Queue FileQueue(){
        return new Queue(rabbitMqProperties.getFileurlqueuename());
    }


    /**
     * 建立交换机和队列的绑定
     * @return
     */
    @Bean
    Binding bindingExchangeOrderDicQueue() {
        return BindingBuilder.bind(directNoteQueue()).to(directNoteExchange());
    }



    @Bean
    Binding bindingUrlQueue() {
        return BindingBuilder.bind(FileQueue()).to(directFileUrlExchange());
    }





    // 声明死信队列A绑定关系
    @Bean
    public Binding deadLetterBinding(){
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange());
    }


}
